package com.yuanqunsoft.alimigration.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.yuanqunsoft.alimigration.bean.RuleBean;
import com.yuanqunsoft.alimigration.bean.RuleImplementBean;
import com.yuanqunsoft.alimigration.dao.IRuleImplementDao;
import com.yuanqunsoft.alimigration.model.Rule;
import com.yuanqunsoft.alimigration.model.RuleImplement;
import com.yuanqunsoft.alimigration.service.IRuleImplementService;

public class RuleImplementServiceImpl implements IRuleImplementService {

    private IRuleImplementDao ruleImplementDao;

    @Override
    public List<RuleBean> getAllRules() {
        // TODO Auto-generated method stub
        return transToBean(ruleImplementDao.getAllRules());
    }

    @Override
    public boolean save(RuleImplementBean rs) {
        // TODO Auto-generated method stub
        RuleImplement ri = this.getRuleImplementFromBean(rs);
        return ruleImplementDao.save(ri);
    }

    @Override
    public boolean delete(RuleImplementBean rs) {
        // TODO Auto-generated method stub
        RuleImplement ri = this.ruleImplementDao.getRuleImplementById(rs.getRuleImpId());
        return ruleImplementDao.delete(ri);
    }

    @Override
    public boolean update(RuleImplementBean rs) {
        // TODO Auto-generated method stub
        RuleImplement ri = this.getRuleImplementFromBean(rs);
        return ruleImplementDao.update(ri);
    }

    @Override
    public List<RuleImplementBean> getAllRuleImplement() {
        // TODO Auto-generated method stub
        return listTransToBean(ruleImplementDao.getAllRuleImplement());
    }

    @Override
    public List<RuleImplementBean> getAllRuleImplement(int currentPage, int pageSize) {
        // TODO Auto-generated method stub
        return listTransToBean(ruleImplementDao.queryPage(currentPage, pageSize));
    }
    @Override
    public RuleImplementBean getRuleImplementById(int id) {
        // TODO Auto-generated method stub
        return transToBean(ruleImplementDao.getRuleImplementById(id));
    }

    @Override
    public int getAllPage() {
        // TODO Auto-generated method stub
        return ruleImplementDao.getAllPage();
    }

    @Override
    public void selectRules(RuleImplementBean rs, Rule ruleBean) {
        // TODO Auto-generated method stub
        Rule rule = this.ruleImplementDao.getRuleById(ruleBean.getRulId());
        RuleImplement ri = getRuleImplementFromBean(rs);
        ri.setRule(rule);
        this.ruleImplementDao.update(ri);
    }

    public IRuleImplementDao getRuleImplementDao() {
        return ruleImplementDao;
    }

    public void setRuleImplementDao(IRuleImplementDao ruleImplementDao) {
        this.ruleImplementDao = ruleImplementDao;
    }

    @Override
    public boolean isInfoExisted(RuleBean rule, String className, int parseEngineType) {
        // TODO Auto-generated method stub
        List<RuleImplement> ruleImpls = this.ruleImplementDao.getAllRuleImplement();
        Iterator<RuleImplement> iterator = ruleImpls.iterator();
        RuleImplement rm;
        while(iterator.hasNext()){
            rm = iterator.next();
           
            if(rm.getRule().getRulId() == rule.getRulId() && rm.getClassName().equals(className) 
                    && rm.getParseEngineType().intValue() == parseEngineType){
                
                return true;
            }
        }
        return false;
    }

    @Override
    public List<RuleImplementBean> getRuleImplementByRuleId(int id) {
        // TODO Auto-generated method stub
        List<RuleImplementBean> beans=new ArrayList<RuleImplementBean>();
        List<RuleImplement> ruleImplements=ruleImplementDao.getRuleImplementByRuleId(id);
        for(RuleImplement ruleImplement:ruleImplements){
            RuleImplementBean bean=new RuleImplementBean();
            bean.setClassName(ruleImplement.getClassName());
            bean.setParseEngineType(ruleImplement.getParseEngineType());
            bean.setRuleImpId(ruleImplement.getRuleImpId());
            bean.setRulId(ruleImplement.getRule().getRulId());
            bean.setRulName(ruleImplement.getRule().getRulName());
            beans.add(bean);
        }
        return beans;
    }

    public List<RuleImplementBean> listTransToBean(List<RuleImplement> ris) {
        List<RuleImplementBean> ribs = new ArrayList<RuleImplementBean>();
        Iterator<RuleImplement> i = ris.iterator();
        RuleImplement r;
        RuleImplementBean rib;
        while(i.hasNext()){
           r =  i.next();
           rib = new RuleImplementBean();
           rib.setClassName(r.getClassName());
           rib.setParseEngineType(r.getParseEngineType());
           rib.setRuleImpId(r.getRuleImpId());
           rib.setRulId(r.getRule().getRulId());
           rib.setRulName(r.getRule().getRulName());
           ribs.add(rib);
        }
        return ribs;
    }

    public RuleImplement getRuleImplementFromBean(RuleImplementBean r){
        Rule rule = this.ruleImplementDao.getRuleById(r.getRulId());
        RuleImplement rib = new RuleImplement();
        rib.setClassName(r.getClassName());
        rib.setParseEngineType(r.getParseEngineType());
        rib.setRuleImpId(r.getRuleImpId());
        rib.setRule(rule);
        return rib;
    }
    public RuleImplementBean transToBean(RuleImplement r){
        
        RuleImplementBean rib = new RuleImplementBean();
        rib.setClassName(r.getClassName());
        rib.setParseEngineType(r.getParseEngineType());
        rib.setRuleImpId(r.getRuleImpId());
        rib.setRulId(r.getRule().getRulId());
        rib.setRulName(r.getRule().getRulName());
        return rib;
    }
    
    public List<RuleBean> transToBean(List<Rule> rules){
        Iterator<Rule> i = rules.iterator();
        List<RuleBean> rbs = new ArrayList<RuleBean>();
        Rule rule;
        RuleBean rb;
        while(i.hasNext()){
            rule = i.next();
            rb = new RuleBean();
            rb.setRulAllFurtherDayEach(rule.getRulAllFurtherDayEach());
            rb.setRulCategory(rule.getRulCategory());
            rb.setRulDescription(rule.getRulDescription());
            rb.setRulEnabled(rule.getRulEnabled());
            rb.setRulFirstAlerts(rule.getRulFirstAlerts());
            rb.setRulFirstDaysEach(rule.getRulFirstDaysEach());
            rb.setRulNextAlerts(rule.getRulNextAlerts());
            rb.setRulNextDaysEach(rule.getRulNextDaysEach());
            rb.setRulId(rule.getRulId());
            rb.setRulName(rule.getRulName());
            rb.setRulSeverity(rule.getRulSeverity());
            rbs.add(rb);
        }
       return rbs;
    }
}
